set -e
set -o pipefail

if [ -z "${USE_APPROACH_BASELINE_FOR_RETRACTION}" ]; then USE_APPROACH_BASELINE_FOR_RETRACTION="false"; fi
if [ -z "${FILE_FORMAT}" ]; then FILE_FORMAT="nanoscope"; fi
if [ -z "${THREADS}" ]; then THREADS=1; fi
if [ -z "${MIN_INTERPEAKS}" ]; then MIN_INTERPEAKS=3; fi
if [ -z "${CL_START_NM}" ]; then CL_START_NM=15.0; fi
if [ -z "${CL_END_NM}" ]; then CL_END_NM=23.0; fi
if [ -z "${MIN_DETACHMENT_FORCE}" ]; then MIN_DETACHMENT_FORCE=200.0; fi
if [ -z "${DETACHMENT_STRICTLY_LARGEST}" ]; then DETACHMENT_STRICTLY_LARGEST="True"; fi
if [ -z "${INPUT_PATH_FILENAME}" ]; then INPUT_PATH_FILENAME=filtered_${DETACHMENT_STRICTLY_LARGEST}_${MIN_DETACHMENT_FORCE}_${MIN_INTERPEAKS}_${CL_START_NM}_${CL_END_NM}.curveStretchPlaylist; fi
if [ -z "${MIN_DELTA_CL_NM}" ]; then MIN_DELTA_CL_NM=0; fi
if [ -z "${MAX_DELTA_CL_NM}" ]; then MAX_DELTA_CL_NM=50; fi
if [ -z "${EPS2PDF}" ]; then EPS2PDF="ps2pdf"; fi
if [ -z "${AMNESTIC}" ]; then AMNESTIC="true"; fi
if [ -z "${WATCH}" ]; then WATCH="false"; fi
if [ -z "${SVG_DEF_SCALE}" ]; then SVG_DEF_SCALE=1.0; fi
if [ -z "${SVG_ITERATION_OFFSET_PN}" ]; then SVG_ITERATION_OFFSET_PN=200.0; fi
if [ -z "${ITERATION}" ]; then ITERATION=0; fi
if [ -z "${WELL_SPACED_AT_END}" ]; then WELL_SPACED_AT_END=1; fi

# BATCH_CLASSPATH for debugging: mvn dependency:build-classpath plus target/classes
if [ -z "${BATCH_CLASSPATH}" ]; then BATCH_CLASSPATH="target/refolding-core-0.1-jar-with-dependencies.jar"; fi

fixedDeflectionSensitivityNmPerV=$DEFL_SENS; fixedSpringConstantNPerM=$K; for d in $DIRS; do p=$(dirname ${d}); b=$(basename ${d})${fixedDeflectionSensitivityNmPerV//*/_${fixedDeflectionSensitivityNmPerV}}${fixedSpringConstantNPerM//*/_${fixedSpringConstantNPerM}}; cd ~/work/rcs/refolding-read-only/core && nice -n 10 java -ea -cp ${BATCH_CLASSPATH} -Djava.awt.headless=true it.unibo.refolding.pico.BatchApproachRetractionAnalysis --iteration=${ITERATION} --svgDefScale=${SVG_DEF_SCALE} --svgIterationOffsetPn=${SVG_ITERATION_OFFSET_PN} --fileFormat=${FILE_FORMAT} --laxApproachContactPoint=false --useApproachBaselineForRetraction=${USE_APPROACH_BASELINE_FOR_RETRACTION} --threads=${THREADS} --defLsbVsZLsbSlope= --minPeakForcePn=0 --minPeakForceRelativeToEmpiricalBaselinePn=0 --medianAbsoluteDeviationLowerBoundPn=0 --leftMostlyHigherWindowSizeNm=0 --leftMostlyHigherFraction=0 --leftMostlyHigherPn=0 --rightMuchHigherThanAllPn=0 --rightMuchHigherPn=0 --minPeakContourLengthNm=0 --minContourLengthDeltaNm=0 --atMostOnePeakWindowNm=0 --dataDir= --inputPathList=${p}/analysis/${b}_analysis/${INPUT_PATH_FILENAME} --wildcardPattern=glob:** --retractionPlaylist=${p}/analysis/${b}_analysis/${b}_selected.repr --hookePlaylist=${p}/analysis/${b}_analysis/${b}_selected.hkp --infoPath=${p}/analysis/${b}_analysis/${b}_selected.info --svgDir=${p}/analysis/${b}_analysis/${b}_svg --resume=false --contactPointAndSlopeDir=${p}/analysis/${b}_analysis/${b}_cps --peakSlopesDir=${p}/analysis/${b}_analysis/${b}_psi --peakForceStrategy=raw --temperatureK=301.15 --persistenceLengthNm=0.35 --clRangeStartNm=0 --clRangeEndNm=Infinity --wellSpacedAtEnd=${WELL_SPACED_AT_END} --allowSkipped=0 --peakCsvPath=${p}/analysis/${b}_analysis/${b}_selected_peaks.csv --watch=${WATCH} --fixedSvgPath=${p}/analysis/${b}_analysis/fixed.svg --svgWildcardPattern=glob:** --approachSkipDistanceNm=75 --junctionPointsDir=${p}/analysis/${b}_analysis/${b}_jp --filteredHookePlaylist= --filteredRetractionPlaylist= --fixedDeflectionSensitivityNmPerV=${fixedDeflectionSensitivityNmPerV} --fixedSpringConstantNPerM=${fixedSpringConstantNPerM} --amnestic=${AMNESTIC} --fetchRefoldBreakParams= --loopParams= --allowedTimeRelativeError=&& cd ~/work/rcs/refolding-read-only/core/src/main/python && python delta_cl_stats.py --processed_playlist=${p}/analysis/${b}_analysis/${b}_selected.repr --curve_stretch_playlist= --min_delta_cl_nm=${MIN_DELTA_CL_NM} --max_delta_cl_nm=${MAX_DELTA_CL_NM} --min_rupture_force_pn=0 --max_rupture_force_pn=10000 --delta_force_filename=${p}/analysis/${b}_analysis/${b}_selected_force.csv --scatterplot_filename=${p}/analysis/${b}_analysis/${b}_selected_scatter.eps && (cd ${p}/analysis/${b}_analysis && ${EPS2PDF} ${b}_selected_scatter.eps); done
